//
//  DownLoaderTableViewController.m
//  Slice
//
//  Created by lanou on 16/6/23.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "DownLoaderTableViewController.h"
#import "DownLoad.h"
@interface DownLoaderTableViewController ()

@end

@implementation DownLoaderTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"已下载" style:UIBarButtonItemStylePlain target:self action:@selector(areadyLoad)];
    
    self.navigationItem.rightBarButtonItem = item;
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DownLoaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"123"];
//    接收监听状态的通知
   [ [NSNotificationCenter defaultCenter] addObserver:self.tableView selector:@selector(reloadData) name:downLoadFinishNotification object:nil];
}

//已下载按钮动作
- (void) areadyLoad {
    AreadyLoadTableViewController *vc = [[AreadyLoadTableViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [DownLoadMaqnager shareInstace].taskArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DownLoaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    DownLoad *load = [DownLoadMaqnager shareInstace].taskArray[indexPath.row];
    
    cell.titleLable.text = load.saveFileName;
    
    load.sendValue = ^(int64_t currentMB, int64_t totalMB) {
         cell.progressView.progress = currentMB / (float)totalMB;
    };
    
    [cell.pauseBtn addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    cell.tag = 3000 + indexPath.row;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < [DownLoadMaqnager shareInstace].taskArray.count) {
        //    取出对应的下载对象
        DownLoad *load = [DownLoadMaqnager shareInstace].taskArray[indexPath.row];
        load.sendValue  = nil;
    }
}
    







//按钮
- (void)pause:(UIButton *)btn {
    DownLoad *load  = [DownLoadMaqnager shareInstace].taskArray[btn.tag - 3000];
    if (load.isLoading) {
        [load pause];
        [btn setTitle:@"继续" forState:UIControlStateNormal];
    } else {
        [load beginLoad];
        [btn setTitle:@"暂停" forState:UIControlStateNormal];
    }
}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
